package com.zhj.bz.datastructure.queue;

/**
 * @author zhj
 */
public class ArrayQueue {

    /*
    最大容量
     */
    private int maxSize;

    /*
    队列头的前一个位置
     */
    private int front;

    /*
    队列尾
     */
    private int rear;

    /*
    队列
     */
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;
    }

    public boolean isFull() {
        return rear == maxSize -1;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 添加数据
     * @param n
     */
    public void addData(int n) {
        if (isFull()) {
            System.out.println("队列已满！");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    public int getData() {
        if (isEmpty()) {
            throw new RuntimeException("队列空，无数据可取！");
        }
        front++;
        return arr[front];
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        System.out.println("队列数据为：");
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，没有数据");
        }
        return arr[front + 1];
    }
}
